package my.merborn.ordersysteam105.controllers;

import my.merborn.ordersysteam105.models.dao.MenuMapper;
import my.merborn.ordersysteam105.models.pojo.Menu;
import my.merborn.ordersysteam105.models.services.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class MenuController {
    @Autowired
    MenuService ms;

    @Autowired
    MenuMapper mm;

    @GetMapping("/updatemenu.do")
    public String updatePrice(@RequestParam String mid,@RequestParam String updateprice){
        Menu select = mm.selectByPrimaryKey(Integer.parseInt(mid));
        select.setmPrice(Double.parseDouble(updateprice));
        System.out.println(select);
        ms.updateFood(select);
        return "redirect:menusmanage.html";
    }

    @RequestMapping("/menusmanage.html")
    public ModelAndView menusManagers(@RequestParam(required = false) ModelAndView mv){
        if(mv == null){
            mv = new ModelAndView("menusmanage.html");
            List<Menu> menuList = ms.getALLByPageable(new PageRequest(0,5));
            mv.addObject("menuList",menuList);
            mv.addObject("current",0);
        }
       // mv.setViewName("menusmanage.html");
        return mv;
    }

    @RequestMapping("/menusmanage.next")
    public ModelAndView menusManagersNext(@PageableDefault(page = 0,size = 5)Pageable pageable){
        ModelAndView mv = new ModelAndView("menusmanage.html");
        List<Menu> allByPageable = ms.getALLByPageable(pageable);
        mv.addObject("current",pageable.getPageNumber());
        mv.addObject("menuList",allByPageable);
        return mv;
    }
    @RequestMapping("/menusmanage.last")
    public ModelAndView menusManagersLast(@PageableDefault(page = 0,size = 5) Pageable pageable){
        ModelAndView mv = new ModelAndView("menusmanage.html");
        List<Menu> allByPageable = ms.getALLByPageable(pageable);
        mv.addObject("current",pageable.getPageNumber());
        mv.addObject("menuList",allByPageable);
        return mv;
    }


    @PostMapping("/newmenu.do")
    public String newMenu(Menu menu){
        menu.setmCount(0);
        ms.addNewFood(menu);
        return "redirect:menusmanage.html";
    }

    @GetMapping("/deletemenu.do")
    public String delete(@RequestParam String mid){
        Menu menu = new Menu();
        menu.setmId(Integer.parseInt(mid));
        System.out.println(menu);
        ms.deleteFood(menu);
        return "redirect:menusmanage.html";
    }
}
